 package client;
 import client.inventory.Equip;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.io.Serializable;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import server.MapleItemInformationProvider;
 import server.quest.MapleQuest;
 import tools.Pair;
 import tools.data.output.MaplePacketLittleEndianWriter;

 public final class MonsterBook implements Serializable {
/*  22 */   private final int level = 0; private static final long serialVersionUID = 7179541993413738569L;
/*  23 */   private final List<Integer> cardItems = new ArrayList<>();
/*  24 */   private final Map<Integer, Pair<Integer, Boolean>> sets = new HashMap<>();
   private boolean changed = false;
/*  26 */   private int currentSet = -1;

   private int setScore;

   private int finishedSets;
   private Map<Integer, Integer> cards;

   public MonsterBook() {}

   public MonsterBook(Map<Integer, Integer> cards, MapleCharacter chr) {
/*  36 */     this.cards = cards;
/*  37 */     calculateItem();
/*  38 */     calculateScore();

/*  40 */     MapleQuestStatus stat = chr.getQuestNoAdd(MapleQuest.getInstance(122800));
/*  41 */     if (stat != null && stat.getCustomData() != null) {
/*  42 */       this.currentSet = Integer.parseInt(stat.getCustomData());
/*  43 */       if (!this.sets.containsKey(Integer.valueOf(this.currentSet)) || !((Boolean)((Pair)this.sets.get(Integer.valueOf(this.currentSet))).right).booleanValue()) {
/*  44 */         this.currentSet = -1;
       }
     }
   }

   public static MonsterBook loadCards(int charid, MapleCharacter chr) throws SQLException {
/*  50 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  51 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM monsterbook WHERE charid = ? ORDER BY cardid ASC");
/*  52 */       ps.setInt(1, charid);
/*  53 */       ResultSet rs = ps.executeQuery();
/*  54 */       Map<Integer, Integer> cards = new LinkedHashMap<>();

/*  56 */       while (rs.next()) {
/*  57 */         cards.put(Integer.valueOf(rs.getInt("cardid")), Integer.valueOf(rs.getInt("level")));
       }
/*  59 */       rs.close();
/*  60 */       ps.close();
/*  61 */       return new MonsterBook(cards, chr);
     }
   }









































   public byte calculateScore() {
/* 106 */     return 0;
   }


   public void writeCharInfoPacket(MaplePacketLittleEndianWriter mplew) {
/* 111 */     List<Integer> cardSize = new ArrayList<>(10);
/* 112 */     for (int i = 0; i < 10; i++)
/* 113 */       cardSize.add(Integer.valueOf(0)); 
     Iterator<Integer> iterator;
/* 115 */     for (iterator = this.cardItems.iterator(); iterator.hasNext(); ) { int x = ((Integer)iterator.next()).intValue();
/* 116 */       cardSize.set(0, Integer.valueOf(((Integer)cardSize.get(0)).intValue() + 1));
/* 117 */       cardSize.set(x / 1000 % 10 + 1, Integer.valueOf(((Integer)cardSize.get(x / 1000 % 10 + 1)).intValue() + 1)); }

/* 119 */     for (iterator = cardSize.iterator(); iterator.hasNext(); ) { int j = ((Integer)iterator.next()).intValue();
/* 120 */       mplew.writeInt(j); }

/* 122 */     mplew.writeInt(this.setScore);
/* 123 */     mplew.writeInt(this.currentSet);
/* 124 */     mplew.writeInt(this.finishedSets);
   }

   public void writeFinished(MaplePacketLittleEndianWriter mplew) {
/* 128 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 129 */     mplew.write(1);
/* 130 */     mplew.writeShort(this.cardItems.size());
/* 131 */     List<Integer> mbList = new ArrayList<>(ii.getMonsterBookList());
/* 132 */     Collections.sort(mbList);
/* 133 */     int fullCards = mbList.size() / 8 + ((mbList.size() % 8 > 0) ? 1 : 0);
/* 134 */     mplew.writeShort(fullCards);

/* 136 */     for (int i = 0; i < fullCards; i++) {
/* 137 */       int currentMask = 1, maskToWrite = 0;
/* 138 */       for (int y = i * 8; y < i * 8 + 8 && 
/* 139 */         mbList.size() > y; y++) {


/* 142 */         if (this.cardItems.contains(mbList.get(y))) {
/* 143 */           maskToWrite |= currentMask;
         }
/* 145 */         currentMask *= 2;
       }
/* 147 */       mplew.write(maskToWrite);
     }

/* 150 */     int fullSize = this.cardItems.size() / 2 + ((this.cardItems.size() % 2 > 0) ? 1 : 0);
/* 151 */     mplew.writeShort(fullSize);
/* 152 */     for (int j = 0; j < fullSize; j++) {
/* 153 */       mplew.write((j == this.cardItems.size() / 2) ? 1 : 17);
     }
   }

   public void writeUnfinished(MaplePacketLittleEndianWriter mplew) {
/* 158 */     mplew.write(0);
/* 159 */     mplew.writeShort(this.cardItems.size());
/* 160 */     for (Iterator<Integer> iterator = this.cardItems.iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 161 */       mplew.writeShort(i % 10000);
/* 162 */       mplew.write(1); }

   }

   public void calculateItem() {
/* 167 */     this.cardItems.clear();
/* 168 */     for (Map.Entry<Integer, Integer> s : this.cards.entrySet()) {
/* 169 */       addCardItem(((Integer)s.getKey()).intValue(), ((Integer)s.getValue()).intValue());
     }
   }

   public void addCardItem(int key, int value) {
/* 174 */     if (value >= 2) {
/* 175 */       Integer x = MapleItemInformationProvider.getInstance().getItemIdByMob(key);
/* 176 */       if (x != null && x.intValue() > 0) {
/* 177 */         this.cardItems.add(x);
       }
     }
   }














   public void modifyBook(Equip eq) {}













   public int getSetScore() {
/* 210 */     return this.setScore;
   }

   public int getLevel() {
/* 214 */     return 0;
   }

   public int getSet() {
/* 218 */     return this.currentSet;
   }

   public boolean changeSet(int c) {
/* 222 */     if (this.sets.containsKey(Integer.valueOf(c)) && ((Boolean)((Pair)this.sets.get(Integer.valueOf(c))).right).booleanValue()) {
/* 223 */       this.currentSet = c;
/* 224 */       return true;
     }
/* 226 */     return false;
   }

   public void changed() {
/* 230 */     this.changed = true;
   }

   public Map<Integer, Integer> getCards() {
/* 234 */     return this.cards;
   }

   public int getSeen() {
/* 238 */     return this.cards.size();
   }

   public int getCaught() {
/* 242 */     int ret = 0;
/* 243 */     for (Iterator<Integer> iterator = this.cards.values().iterator(); iterator.hasNext(); ) { int i = ((Integer)iterator.next()).intValue();
/* 244 */       if (i >= 2) {
/* 245 */         ret++;
       } }

/* 248 */     return ret;
   }

   public int getLevelByCard(int cardid) {
/* 252 */     return (this.cards.get(Integer.valueOf(cardid)) == null) ? 0 : ((Integer)this.cards.get(Integer.valueOf(cardid))).intValue();
   }

   public void saveCards(DruidPooledConnection con, int charid) throws SQLException {
/* 256 */     if (!this.changed) {
       return;
     }
/* 259 */     PreparedStatement ps = con.prepareStatement("DELETE FROM monsterbook WHERE charid = ?");
/* 260 */     ps.setInt(1, charid);
/* 261 */     ps.execute();
/* 262 */     ps.close();
/* 263 */     this.changed = false;
/* 264 */     if (this.cards.isEmpty()) {
       return;
     }

/* 268 */     boolean first = true;
/* 269 */     StringBuilder query = new StringBuilder();

/* 271 */     for (Map.Entry<Integer, Integer> all : this.cards.entrySet()) {
/* 272 */       if (first) {
/* 273 */         first = false;
/* 274 */         query.append("INSERT INTO monsterbook VALUES (DEFAULT,");
       } else {
/* 276 */         query.append(",(DEFAULT,");
       }
/* 278 */       query.append(charid);
/* 279 */       query.append(",");
/* 280 */       query.append(all.getKey());
/* 281 */       query.append(",");
/* 282 */       query.append(all.getValue());
/* 283 */       query.append(")");
     }
/* 285 */     ps = con.prepareStatement(query.toString());
/* 286 */     ps.execute();
/* 287 */     ps.close();
   }

   public boolean monsterCaught(MapleClient c, int cardid, String cardname) {
/* 291 */     if (!this.cards.containsKey(Integer.valueOf(cardid)) || ((Integer)this.cards.get(Integer.valueOf(cardid))).intValue() < 2) {
/* 292 */       this.changed = true;
/* 293 */       c.getPlayer().dropMessage(-6, "Book entry updated - " + cardname);

/* 295 */       this.cards.put(Integer.valueOf(cardid), Integer.valueOf(2));
/* 296 */       return true;
     }
/* 298 */     return false;
   }

   public void monsterSeen(MapleClient c, int cardid, String cardname) {
/* 302 */     if (this.cards.containsKey(Integer.valueOf(cardid))) {
       return;
     }
/* 305 */     this.changed = true;

/* 307 */     c.getPlayer().dropMessage(-6, "New book entry - " + cardname);
/* 308 */     this.cards.put(Integer.valueOf(cardid), Integer.valueOf(1));
   }
 }


/* Location:              C:\PGX\Server.jar!\client\MonsterBook.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */